package com.budwk.app.miniapp.controllers.admin;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.date.DateUtil;
import com.budwk.app.miniapp.models.WxVolunteerSMPassCheck;
import com.budwk.starter.common.result.Result;
import com.budwk.starter.common.result.ResultCode;
import com.budwk.starter.common.page.Pagination;
import com.budwk.starter.common.page.PageUtil;
import com.budwk.starter.log.annotation.SLog;
import com.budwk.starter.security.utils.SecurityUtil;
import com.budwk.app.miniapp.models.WxVolunteer;
import com.budwk.app.miniapp.services.WxvolunteerService;
import jodd.util.StringUtil;
import org.nutz.dao.Cnd;
import org.nutz.dao.Sqls;
import org.nutz.dao.sql.Sql;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.adaptor.JsonAdaptor;
import org.nutz.mvc.annotation.*;
import org.nutz.lang.Strings;
import com.budwk.starter.common.openapi.annotation.*;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

@IocBean
@At("/platform/miniapp/volunteer")
@ApiDefinition(tag = "微信小程序-志愿者信息")
@Slf4j
public class WxvolunteerController {
    @Inject
    private WxvolunteerService wxVolunteerService;

    @At("/list")
    @Ok("json")
    @POST
    @ApiOperation(name = "分页查询志愿者信息")
    @ApiFormParams(
            value = {
                    @ApiFormParam(name = "pageNo", example = "1", description = "页码", type = "integer"),
                    @ApiFormParam(name = "pageSize", example = "10", description = "页大小", type = "integer"),
                    @ApiFormParam(name = "pageOrderName", example = "createdAt", description = "排序字段"),
                    @ApiFormParam(name = "pageOrderBy", example = "descending", description = "排序方式")
            },
            implementation = WxVolunteer.class
    )
    @ApiResponses(
            implementation = WxVolunteer.class
    )
    @SaCheckLogin
    public Result<?> list(@Param("..") WxVolunteer bean,
                          @Param("pageNo") int pageNo,
                          @Param("pageSize") int pageSize,
                          @Param("pageOrderName") String pageOrderName,
                          @Param("pageOrderBy") String pageOrderBy) {
        Cnd cnd = Cnd.NEW();
        if (Strings.isNotBlank(pageOrderName) && Strings.isNotBlank(pageOrderBy)) {
            cnd.orderBy(pageOrderName, PageUtil.getOrder(pageOrderBy));
        }
        if (bean.getName() != null && Strings.isNotBlank(bean.getName().toString())) {
            cnd.and("name", "=", bean.getName());
        }
        if (bean.getSexual() != null && Strings.isNotBlank(bean.getSexual().toString())) {
            cnd.and("sexual", "=", bean.getSexual());
        }
        if (bean.getPhone() != null && Strings.isNotBlank(bean.getPhone().toString())) {
            cnd.and("phone", "=", bean.getPhone());
        }
        if (bean.getVxprovinces() != null && Strings.isNotBlank(bean.getVxprovinces().toString())) {
            cnd.and("vxprovinces", "=", bean.getVxprovinces());
        }
        if (bean.getVxcities() != null && Strings.isNotBlank(bean.getVxcities().toString())) {
            cnd.and("vxcities", "=", bean.getVxcities());
        }
        if (bean.getYears() != null && Strings.isNotBlank(bean.getYears().toString())) {
            cnd.and("years", "=", bean.getYears());
        }
        if (bean.getProvinces() != null && Strings.isNotBlank(bean.getProvinces().toString())) {
            cnd.and("provinces", "=", bean.getProvinces());
        }
        if (bean.getCities() != null && Strings.isNotBlank(bean.getCities().toString())) {
            cnd.and("cities", "=", bean.getCities());
        }
        if (bean.getDistricts() != null && Strings.isNotBlank(bean.getDistricts().toString())) {
            cnd.and("districts", "=", bean.getDistricts());
        }
        if (bean.getTownships() != null && Strings.isNotBlank(bean.getTownships().toString())) {
            cnd.and("townships", "=", bean.getTownships());
        }
        if (bean.getServicetype() != null && Strings.isNotBlank(bean.getServicetype().toString())) {
            cnd.and("servicetype", "=", bean.getServicetype());
        }
        if (bean.getServicetime() != null && Strings.isNotBlank(bean.getServicetime().toString())) {
            cnd.and("servicetime", "=", bean.getServicetime());
        }
        if (bean.getRegistrationtime() != null && Strings.isNotBlank(bean.getRegistrationtime().toString())) {
            cnd.and("registrationtime", "=", bean.getRegistrationtime());
        }
       /* if (bean.getCreateTime() != null && Strings.isNotBlank(bean.getCreateTime().toString())) {
            cnd.and("createTime", "=", bean.getCreateTime());
        }*/
        if (bean.getUpdateBy() != null && Strings.isNotBlank(bean.getUpdateBy().toString())) {
            cnd.and("updatedby", "=", bean.getUpdateBy());
        }
        if (bean.getUpdateDat() != null && Strings.isNotBlank(bean.getUpdateDat().toString())) {
            cnd.and("updatedat", "=", bean.getUpdateDat());
        }
        if (bean.getDelFlag() != null && Strings.isNotBlank(bean.getDelFlag().toString())) {
            cnd.and("delflag", "=", bean.getDelFlag());
        }
        if (bean.getCreateBy() != null && Strings.isNotBlank(bean.getCreateBy().toString())) {
            cnd.and("createdby", "=", bean.getCreateBy());
        }
        if (bean.getCreateDat() != null && Strings.isNotBlank(bean.getCreateDat().toString())) {
            cnd.and("createdat", "=", bean.getCreateDat());
        }
        if (bean.getOpenid() != null && Strings.isNotBlank(bean.getOpenid().toString())) {
            cnd.and("openid", "=", bean.getOpenid());
        }
        if (bean.getPushstatus() != null && Strings.isNotBlank(bean.getPushstatus().toString())) {
            cnd.and("pushstatus", "=", bean.getPushstatus());
        }
        if (bean.getPushtimes() != null && Strings.isNotBlank(bean.getPushtimes().toString())) {
            cnd.and("pushtimes", "=", bean.getPushtimes());
        }
        if (bean.getAllServiceTypeFlag() != null && Strings.isNotBlank(bean.getAllServiceTypeFlag().toString())) {
            cnd.and("all_servicetype_flag", "=", bean.getAllServiceTypeFlag());
        }
        return Result.data(wxVolunteerService.listPage(pageNo, pageSize, cnd));
    }

    @At("/create")
    @Ok("json")
    @AdaptBy(type = JsonAdaptor.class)
    @POST
    @ApiOperation(name = "志愿者申请")
    @ApiFormParams(
            implementation = WxVolunteer.class
    )
    @ApiResponses
    @SaCheckLogin
    public Result<?> create(@Param("..") WxVolunteer wxVolunteer, HttpServletRequest req) {
        wxVolunteer.setCreateBy(SecurityUtil.getUserId());
        wxVolunteer.setUpdateBy(SecurityUtil.getUserId());
        // 添加时间
        wxVolunteer.setCreateDat(DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
        // 修改时间
        wxVolunteer.setUpdateDat(DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
        //wxVolunteer.setCreateTime(Timestamp.valueOf(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())));
        wxVolunteerService.insert(wxVolunteer);
        return Result.success();
    }

    @At("/update")
    @Ok("json")
    @POST
    @ApiOperation(name = "修改志愿者信息")
    @ApiFormParams(
            implementation = WxVolunteer.class
    )
    @ApiResponses
    @SLog(tag = "修改志愿者信息", value = "${args[0].id}")
    @SaCheckLogin
    public Result<?> update(@Param("..") WxVolunteer wxVolunteer, HttpServletRequest req) {
        wxVolunteer.setUpdateBy(SecurityUtil.getUserId());
        wxVolunteer.setUpdateDat(DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
        wxVolunteerService.updateIgnoreNull(wxVolunteer);
        return Result.success();
    }

    @At("/get")
    @Ok("json")
    @POST
    @ApiOperation(name = "获取志愿者详细信息")
    @ApiFormParams(
            {
                    @ApiFormParam(name = "id", description = "唯一标识", required = true, check = true)
            }
    )
    @SaCheckLogin
    public Result<?> getData(@Param("id") String id, HttpServletRequest req) {
        WxVolunteer wxVolunteer = wxVolunteerService.fetch(id);
        if (wxVolunteer == null) {
            return Result.error(ResultCode.NULL_DATA_ERROR);
        }
        return Result.data(wxVolunteer);
    }

    @At("/delete")
    @Ok("json")
    @POST
    @ApiOperation(name = "删除志愿者信息")
    @ApiFormParams(
            {
                    @ApiFormParam(name = "id", description = "唯一标识", required = true, check = true)
            }
    )
    @SLog(tag = "删除志愿者信息", value = "${args[0]}")
    @SaCheckLogin
    public Result<?> delete(@Param("id") String id, HttpServletRequest req) {
        WxVolunteer wxVolunteer = wxVolunteerService.fetch(id);
        if (wxVolunteer == null) {
            return Result.error(ResultCode.NULL_DATA_ERROR);
        }
        wxVolunteerService.delete(id);
        return Result.success();
    }

    /**
     * 添加志愿者微信推送信息的次数
     *
     * @param bo bo
     * @return 结果
     */
    @At("/addSMPassCheck")
    @Ok("json")
    @POST
    @ApiOperation(name = "添加志愿者微信推送信息的次数")
    @ApiFormParams(
            implementation = WxVolunteerSMPassCheck.class
    )
    @SLog(tag = "添加志愿者微信推送信息的次数", value = "${args[0]}")
    @SaCheckLogin
    public Result<Integer> addSMPassCheck(@Param("..") WxVolunteerSMPassCheck bo) {
        Integer passCheckCount = wxVolunteerService.addSMPassCheck(bo);
        return Result.success(passCheckCount);
    }

    /**
     * 获取订阅消息次数
     *
     * @return 结果
     */
    @At("/getSMPassCheckCount/{openId}")
    @Ok("json")
    @GET
    @ApiOperation(name = "获取订阅消息次数")
    @SaCheckLogin
    public Result<?> getSMPassCheckCount(@Param("openId") String openId) {
        if (StringUtil.isNotBlank(openId)) {
            Integer pushtimes = wxVolunteerService.getSMPassCheckCount(openId);
            return Result.data(pushtimes);
        }
        return Result.data(0);
    }

    /**
     * 统计事件数量
     *
     * @return 结果
     */
    @At("/getHomePageEventStatistic")
    @Ok("json")
    @GET
    @ApiOperation(name = "统计事件数量")
    @SaCheckLogin
    public Result<?> getHomePageEventStatistic() {
        return Result.data(wxVolunteerService.getHomePageEventStatistic());
    }
}
